<template>
    <div :style="execGlobalStyle">
        <el-pagination
            v-loading.fullscreen.lock="fullscreenLoading"
            v-model:current-page="currentPageData"
            v-model:page-size="pageSizeData"
            :small="propJson.paginationSizeType === 'small'"
            :background="propJson.isBackground"
            :layout="layout"
            :total="total"
            @change="changePage"
            :page-sizes="pageSizeList"
            :style="execClass"
            :info="currentPage"
            :info2="pageSize"
        />
    </div>
</template>

<script name="PaginationComponent" setup lang="ts">
import {deepCopy} from "@/utils/ywuCommon";
import {doThirdServiceAction} from "@/utils/lowcode/thirdService";
import {computed, defineProps, ref} from "vue";

let props = defineProps(['propJson', 'commonParam']);

let propJson = computed(() => {return props['propJson']});
let commonParam = computed(() => {return props['commonParam']});

let currentPageData = ref(0);
let pageSizeData = ref(0);
let fullscreenLoading = ref(false);

let execClass = computed(() => {
    let resStyle = ""
    if (propJson.value.editFlag) {
        resStyle += "pointer-events: none;";
    }
    return resStyle;
})

let execGlobalStyle = computed(() => {
    let resStyle = "width: 100%;display: flex; align-items: center;";
    resStyle += "justify-content: " + propJson.value.layOutPosition + ";";
    return resStyle;
})

let currentPage = computed(() => {
    // 判断是否为自定义
    if (propJson.value.currentPageType === 'custom') {
        currentPageData.value = parseInt(propJson.value.currentPageCustom)
    } else if (propJson.value.currentPageType === 'variable') {
        for (let i = 0; i < commonParam.value.length; i++) {
            let commonParamInfo = commonParam.value[i]
            if (propJson.value.currentPageVariable === commonParamInfo.variableName) {
                if (commonParamInfo.variableValue === "") {
                    currentPageData.value = 1
                    break;
                }
                currentPageData.value = parseInt(commonParamInfo.variableValue)
                break;
            }
        }
    } else {
        currentPageData.value = 1
    }
})

let pageSize = computed(() => {
    // 判断是否为自定义
    if (propJson.value.pageSizeType === 'custom') {
        pageSizeData.value = parseInt(propJson.value.pageSizeCustom)
    } else if (propJson.value.pageSizeType === 'variable') {
        for (let i = 0; i < commonParam.value.length; i++) {
            let commonParamInfo = commonParam.value[i]
            if (propJson.value.pageSizeVariable === commonParamInfo.variableName) {
                if (commonParamInfo.variableValue === "") {
                    pageSizeData.value = 10
                    break;
                }
                pageSizeData.value = parseInt(commonParamInfo.variableValue)
                break;
            }
        }
    } else {
        pageSizeData.value = 10
    }
})

let total = computed(() => {
    // 判断是否为自定义
    if (propJson.value.totalType === 'custom') {
        return parseInt(propJson.value.totalCustom)
    } else if (propJson.value.totalType === 'variable') {
        for (let i = 0; i < commonParam.value.length; i++) {
            let commonParamInfo = commonParam.value[i]
            if (propJson.value.totalVariable === commonParamInfo.variableName) {
                if (commonParamInfo.variableValue === "") {
                    return 10
                }
                return parseInt(commonParamInfo.variableValue)
            }
        }
    } else {
        return 10
    }
})

let layout = computed(() => {
    let layOutRes = []
    if (propJson.value.paginationLayoutPrevFlag) {
        let index = propJson.value.paginationLayoutPrevNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "prev")
    }
    if (propJson.value.paginationLayoutPagerFlag) {
        let index = propJson.value.paginationLayoutPagerNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "pager")
    }
    if (propJson.value.paginationLayoutNextFlag) {
        let index = propJson.value.paginationLayoutNextNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "next")
    }
    if (propJson.value.paginationLayoutJumperFlag) {
        let index = propJson.value.paginationLayoutJumperNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "jumper")
    }
    if (propJson.value.paginationLayoutSizesFlag) {
        let index = propJson.value.paginationLayoutSizesNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "sizes")
    }
    if (propJson.value.paginationLayoutTotalFlag) {
        let index = propJson.value.paginationLayoutTotalNum - 1;
        // 插入数据
        layOutRes.splice(index, 0, "total")
    }
    let resStr = ""
    for (let i = 0; i < layOutRes.length - 1; i++) {
        let info = layOutRes[i]
        resStr += info + ", "
    }
    if (layOutRes.length > 0) {
        resStr += layOutRes[layOutRes.length - 1]
    }
    return resStr
})

let pageSizeList = computed(() => {
    // 判断是否为空
    if (propJson.value.pageSizeList === "") {
        return [10]
    }
    if (Array.isArray(propJson.value.pageSizeList)) {
        return propJson.value.pageSizeList
    } else {
        return JSON.parse(propJson.value.pageSizeList)
    }
})

async function changePage(index, size) {
    if (propJson.value.editFlag) {
        return;
    }
    if (!propJson.value.editFlag) {
        // fullscreenLoading.value = true;
    }
    // 构建内置变量参数
    let innerParams = propJson.value.pageChangeInnerRequestVariable
    let resList = deepCopy(propJson.value.paginationActionList)
    for (let i = 0; i < resList.length; i++) {
        let action = resList[i]
        if (!action.apiParamList) {
            action.apiParamList = []
        }
        let realIndex;
        if (propJson.value.pageStartIndex === 0 || propJson.value.pageStartIndex === '0') {
            realIndex = index - 1;
        } else {
            realIndex = index;
        }
        let pageIndexVariableInfo = {variableName: innerParams[0].variableName, variableValue: realIndex, type: "custom", variableType: "String", variableParamName: ""}
        action.apiParamList.push(pageIndexVariableInfo)
        let pageSizeVariableInfo = {variableName: innerParams[1].variableName, variableValue: size, type: "custom", variableType: "String", variableParamName: ""}
        action.apiParamList.push(pageSizeVariableInfo)
    }
    // 定义一个标志位，后续同步执行
    let awaitObj = {flag: false, res: true}
    await doThirdServiceAction(resList, awaitObj, commonParam.value, ()=>{fullscreenLoading.value = false;}, ()=>{}, null);
}
</script>

<style scoped lang="less">

</style>